from PyQt5.QtCore import *
from PyQt5.QtGui import QFocusEvent
from examples.example_calculator.calc_conf import *
#from calc_conf import *#--
from examples.example_calculator.calc_node_base import *
#from calc_node_base import *#--
from nodeeditor.utils import dumpException
import examples.example_calculator.tuoZhan as tuoZhan

#此节点点击以后会自动收集鼠标指向的窗口句柄，并附加给tuozhuai模块里面

class CalcGraphicsNode(QDMGraphicsNode):#节点外壳
    def initSizes(self):
        super().initSizes()
        self.width = 200
        self.height = 75
        self.edge_roundness = 6
        self.edge_padding = 0
        self.title_horizontal_padding = 8
        self.title_vertical_padding = 10

    def initAssets(self):
        super().initAssets()
        self.icons = QImage("icons/status_icons.png")

    def paint(self, painter, QStyleOptionGraphicsItem, widget=None):
        super().paint(painter, QStyleOptionGraphicsItem, widget)

        offset = 24.0
        if self.node.isDirty(): offset = 0.0
        if self.node.isInvalid(): offset = 48.0

        painter.drawImage(
            QRectF(-10, -10, 24.0, 24.0),
            self.icons,
            QRectF(offset, 0, 24.0, 24.0)
        )

class CalcShuChuContent(QDMNodeContentWidget):
    def initUI(self):
        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(10,0,10,0)
        self.setLayout(self.layout)
        
        self.edit = QLineEdit("????", self)#句柄显示
        self.edit.setAlignment(Qt.AlignLeft)
        self.edit.setEnabled(False)#禁用文本框
        
        self.button = QPushButton(text="绑定窗体",parent=self)
        
        self.layout.addWidget(self.edit)
        self.layout.addWidget(self.button)

@register_node(OP_NODE_HWND)
class CalcNode_Hwnd(CalcNode):
    icon = "icons/in.png"
    op_code = OP_NODE_HWND
    op_title = "3秒后获取鼠标指向句柄"
    content_label_objname = "calc_node_hwnd"
    GraphicsNode_class = CalcGraphicsNode

    def __init__(self, scene):
        super().__init__(scene, inputs=[], outputs=[])
        self.eval()

    def initInnerClasses(self):
        self.content = CalcShuChuContent(self)
        self.grNode = CalcGraphicsNode(self)
        self.content.button.clicked.connect(self.gethwnd)
    
    def evalImplementation(self):
        s_value = self.content.edit.text()
        self.value = s_value
        self.markDirty(False)
        self.markInvalid(False)

        self.markDescendantsInvalid(False)
        self.markDescendantsDirty()

        self.grNode.setToolTip("")

        self.evalChildren()

        return self.value
    
    def gethwnd(self):
        self.time01 = QTimer()
        self.time01.setInterval(3000)
        self.time01.timeout.connect(self.time02)
        self.time01.start()
        
    def time02(self):
        print("延迟3秒")
        self.time01.stop()
        print(tuoZhan.dm.GetCursorPos())
        hwnd01 = tuoZhan.dm.GetPointWindow(tuoZhan.dm.GetCursorPos()[1],tuoZhan.dm.GetCursorPos()[2])
        tuoZhan.sethwnd(hwnd01)
        self.content.edit.setText(str(hwnd01))